<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>分享详情</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
	<meta name="apple-mobile-web-app-capable" content="yes" />
	<meta name="apple-mobile-web-app-status-bar-style" content="black" />
	<meta name="format-detection" content="telephone=no" />
	<link href="{$Think.config.RESOURCEURL}wx_assets/css/iconfont/font_tuan.css{$Think.config.RESOURCE_VER}" rel="stylesheet">
	<link href="{$Think.config.RESOURCEURL}wx_assets/iconfont/iconfont.css{$Think.config.RESOURCE_VER}" rel="stylesheet">
	<link href="{$Think.config.RESOURCEURL}wx_assets/css/common.css{$Think.config.RESOURCE_VER}" rel="stylesheet" type="text/css">
	<link href="{$Think.config.RESOURCEURL}wx_assets/micontent/materiaDetail.css{$Think.config.RESOURCE_VER}" rel="stylesheet">
	<link href="{$Think.config.RESOURCEURL}wx_assets/micontent/emoji.css{$Think.config.RESOURCE_VER}" rel="stylesheet">
	<script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/zepto.1.1.3.min.js{$Think.config.RESOURCE_VER}"></script>
	<script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/zepto.alert.js{$Think.config.RESOURCE_VER}"></script>
	<script src="{$Think.config.RESOURCEURL}wx_assets/js/util.js{$Think.config.RESOURCE_VER}" type="text/javascript"></script>
	<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
	<script src="{$Think.config.RESOURCEURL}wx_assets/js/wx_share.js{$Think.config.RESOURCE_VER}" type="text/javascript"></script>
	<script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/product/product.js{$Think.config.RESOURCE_VER}"></script>

	<link href="{$Think.config.RESOURCEURL}wx_assets/js/area/mobiscroll.javascript.min.css{$Think.config.RESOURCE_VER}" rel="stylesheet" type="text/css">
	<script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/area/area_array.js{$Think.config.RESOURCE_VER}"></script>
	<script type="text/javascript" src="{$Think.config.RESOURCEURL}wx_assets/js/area/mobiscroll.javascript.min.js{$Think.config.RESOURCE_VER}"></script>
	<style>
		.share-info .tab-list .active {
			color: {$themeColor};
			border-bottom: 2px solid {$themeColor};
		}
	</style>
</head>
<body class="share-info" style="padding: 0 !important;">
 	<div class="share-info-main">
		<ul class="tab-list" style="padding: 0;">
			<li  data-type="share" class="active">
				<div>分享人数</div>
				<div class="share-total"></div>
			</li>
			<li data-type="visit">
				<div>浏览人数</div>
				<div class="visit-total"></div>
			</li>
			<li data-type="order">
				<div>线索人数</div>
				<div class="order-total"></div>
			</li>
		</ul>
		<ul class="content-list js-list" style="padding: 0;"></ul>
 	</div>
	<!-- 列表模板 -->
	<script type="text/html" id="item-tpl">
		<% for(var i = 0; i < list.length; i++) {%>
			<li>
				<div class="left-feild">
					<% if (list[i].headimgurl) {%>
					<img src="<%=list[i].headimgurl%>">
					<%}else{%>
						<i class="iconfont">&#xe678;</i>
					<%}%>
					<% if (list[i].nickname) {%>
						<span><%=list[i].nickname%></span>
					<%}else{%>
						<span>（未关注）</span>
					<%}%>
				</div>
				<% if (type === 'share') {%>
					<div class="right-feild"><span>浏览人数：<%=list[i].uv%></span><span><%=list[i].time%></span></div>
				<%} else {%>
					<div class="right-feild"><%=list[i].time%></div>
				<% } %>
			</li>
		<% } %>
	</script>
<script>
	//格式化时间函数
	function timeParser (time) {
	  var publishTime = Date.parse(time.replace(/-/g, '/')) / 1000
	  var dSeconds
	  var dMinutes
	  var dHours
	  var dDays
	  var timeNow = parseInt(new Date().getTime() / 1000)
	  var d

	  var date = new Date(publishTime * 1000)
	  var Y = date.getFullYear()
	  var M = date.getMonth() + 1
	  var D = date.getDate()
	  var H = date.getHours()
	  var m = date.getMinutes()
	  var s = date.getSeconds()
	  // 小于10的在前面补0
	  if (M < 10) {
	    M = '0' + M
	  }
	  if (D < 10) {
	    D = '0' + D
	  }
	  if (H < 10) {
	    H = '0' + H
	  }
	  if (m < 10) {
	    m = '0' + m
	  }
	  if (s < 10) {
	    s = '0' + s
	  }

	  d = timeNow - publishTime
	  dDays = parseInt(d / 86400)
	  dHours = parseInt(d / 3600)
	  dMinutes = parseInt(d / 60)
	  dSeconds = parseInt(d)

	  if (dDays > 0 && dDays < 3) {
	    return dDays + '天前'
	  } else if (dDays <= 0 && dHours > 0) {
	    return dHours + '小时前'
	  } else if (dHours <= 0 && dMinutes > 0) {
	    return dMinutes + '分钟前'
	  } else if (dSeconds < 60) {
	    if (dSeconds <= 0) {
	      return '刚刚'
	    } else {
	      return dSeconds + '秒前'
	    }
	  } else if (dDays >= 3 && dDays < 30) {
	    return M + '-' + D + ' ' + H + ':' + m
	  } else if (dDays >= 30) {
	    return Y + '-' + M + '-' + D
	  }
	}

	// 解析url函数
    function parseURL(url) {
    	if (!url) {
    		url = location.href
    	}
        var a =  document.createElement('a');
        a.href = url;
        return {
        source: url,
        protocol: a.protocol.replace(':',''),
        host: a.hostname,
        port: a.port,
        query: a.search,
        params: (function(){
            var ret = {},
                seg = a.search.replace(/^\?/,'').split('&'),
                len = seg.length, i = 0, s;
            for (;i<len;i++) {
                if (!seg[i]) { continue; }
                s = seg[i].split('=');
                ret[s[0]] = s[1];
            }
            return ret;
        })(),
        file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
        hash: a.hash.replace('#',''),
        path: a.pathname.replace(/^([^\/])/,'/$1'),
        relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
        segments: a.pathname.replace(/^\//,'').split('/')
        };
    }

	function Page () {
		this.type = 'share'
		this.page = 1;
		this.perNum = 10;
		this.pageCount = Math.ceil({$total} / this.perNum) + 1;
		this.params = JSON.parse(decodeURIComponent(parseURL(window.location.href).params.params));
		console.log(this.pageCount);
	}

	$.extend(Page.prototype, {
		init () {
			this.render(JSON.parse('{$results|json_encode}') || []);
			this.initNum();
			this.bindEvents();
			this.initLoadMore();
		},

		bindEvents () {
			var that = this
			$('.share-info .tab-list li').click(function(){
				$('.share-info .tab-list li').removeClass('active');
				$(this).addClass('active');
				$('.content-list').html('')
				that.type = $(this).data('type');
				that.page = 1;
				that._loadList(that.page);
				$.util.dataEnd.hide();
			});
		},

		initNum () {
			$('.share-total').text('{$shareTotal}');
			$('.visit-total').text('{$visitTotal}');
			$('.order-total').text('{$orderTotal}');
		},

		initLoadMore () {
            var that = this;
            $.util.initDataLoading({
            	me: that,
                lastChildren () {
                    return $(".js-list:last");
                },
                page () {
                    return that.page;
                },
                pageCount () {
                    return that.pageCount;
                }
            });
        },

		_loadList (page, callback) {
			var that = this;
			that.page = page;
			var params = {
                miUid: this.params.miUid,
                moduleId: this.params.moduleId,
                type: this.type,
                start: (this.page - 1) * this.perNum,
                limit: this.perNum
            }
            $.ajax({
                type: "get",
                url: '{$Think.config.SERVER_HOST}call.json?customerId={$customerId}',
                dataType: 'json',
                data: {
                   module: "micontent",
                   method: "getUserMiContentData",
                   params: JSON.stringify(params)
                },
                success (res) {
                	callback && callback();
					if (+res.code === 0) {
						that.pageCount = that.pageCount = Math.ceil(res.total / that.perNum) + 1;
						that.render(res.results || [])
					} else {
						alert(res.msg);
					}
                },
                error: $.proxy(this.handleFetchListInfoError, this)
            });
		},

		render (list) {
			var that = this;
			for (var i = 0; i < list.length; i++) {
				list[i].time = timeParser(list[i].createTime)
			}
			var html = template("item-tpl", {
				list: list,
				type: that.type
			});
			this.page === 1 ? $('.content-list').html(html) : $('.content-list').append(html);
		},

		handleFetchListInfoError () {
			alert('获取广告列表失败，请重试！');
		}
	})

	var page = new Page();
	page.init();
</script>
</body>
</html>